﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace CustomerOrderingSystem
{
    public partial class Purchase : Form
    {
        private string sql = "";
        public Purchase()
        {
            InitializeComponent();
        }

        private void Purchase_Load(object sender, EventArgs e)
        {
            purordDataGridView.DataSource = sql_purchase.QueryDataTable0();
        }

        private void MakeSqlStr()
        {
            {
                //清空上次的查询字符串
                sql = "";
                if (purnoTextBox.Text.Trim() != string.Empty)
                {

                    sql = "and Purno ='" + purnoTextBox.Text.Trim() + "'";
                }
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            MakeSqlStr();
            purordDataGridView.DataSource = sql_purchase.QueryDataTable(sql);
        }

        private void purordDataGridView_TextChanged(object sender, EventArgs e)
        {

        }

        private void button2_Click(object sender, EventArgs e)
        {
            try
            {
                int cnt = sql_purchase.QueryDataTable1(purnoTextBox1.Text);
                if (cnt == 1)
                {
                    MessageBox.Show("更新成功！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    sql_purchase.QueryDataTable2(pnoTextBox.Text, gnoTextBox.Text, purpriceTextBox.Text, purnumTextBox.Text, purnoTextBox1.Text);
                }
                // 添加新记录
                else
                {
                    MessageBox.Show("添加成功！", "提示", MessageBoxButtons.OK);
                    sql_purchase.QueryDataTable3(purnoTextBox1.Text, pnoTextBox.Text, gnoTextBox.Text, purpriceTextBox.Text, purnumTextBox.Text);
                }
                Purchase_Load(null, null);
            }
            finally
            {
                sql_purchase.QueryDataTable4();
            }
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            DialogResult ret = MessageBox.Show("确定要删除记录吗？", "删除", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
            if (ret == DialogResult.Cancel)
            {
                return;
            }

            try
            {

                if (sql_purchase.QueryDataTable5(purnoTextBox1.Text) == 1)
                {
                    MessageBox.Show("删除成功！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                Purchase_Load(null, null);
            }
            finally
            {
                sql_purchase.QueryDataTable4();
            }
        }
    }
}
